// pages/peTest/peTest.js
var app = getApp()
import articles from '../../../utils/physical-data.js';
import * as stands from '../../../utils/standards.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curTab: 0, // tab切换
    technical: [{
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 1,
        value: 'BMI指数',
        key: 'boyBMI',
        name: '（单位：千克/米＾2）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 0,
        value: 'BMI指数',
        key: 'girlBMI',
        name: '（单位：千克/米＾2）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 1,
        value: '肺活量',
        key: 'boyVitalCapacity',
        name: '（单位：毫升）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 0,
        value: '肺活量',
        key: 'girlVitalCapacity',
        name: '（单位：毫升）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 1,
        value: '50米跑',
        key: 'boyRun50',
        name: '（单位：秒）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 0,
        value: '50米跑',
        key: 'girlRun50',
        name: '（单位：秒）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 1,
        value: '坐位体前屈',
        key: 'boySitAndReach',
        name: '（单位：厘米）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 0,
        value: '坐位体前屈',
        key: 'girlSitAndReach',
        name: '（单位：厘米）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6],
        gender: 1,
        value: '1分钟跳绳',
        key: 'boySkip',
        name: '（单位：次）'
      },
      {
        grades: [1, 2, 3, 4, 5, 6],
        gender: 0,
        value: '1分钟跳绳',
        key: 'girlSkip',
        name: '（单位：次）'
      },
      {
        grades: [7, 8, 9, 10, 11, 12],
        gender: 1,
        value: '立定跳远',
        key: 'boyStandingLongJump',
        name: '（单位：厘米）'
      },
      {
        grades: [],
        gender: 0,
        value: '立定跳远',
        key: 'girlStandingLongJump',
        name: '（单位：厘米）'
      },
      {
        grades: [7, 8, 9, 10, 11, 12],
        gender: 1,
        value: '引体向上',
        key: 'boySitupChinup',
        name: '（单位：次）'
      },
      {
        grades: [3, 4, 5, 6],
        gender: 1,
        value: '1分钟仰卧起坐',
        key: 'girlSitupChinup',
        name: '（单位：次）'
      },
      {
        grades: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        gender: 0,
        value: '1分钟仰卧起坐',
        key: 'girlSitupChinup',
        name: '（单位：次）'
      },
      {
        grades: [5, 6],
        gender: 1,
        value: '50*8往返跑',
        key: 'boyEnduranceRun',
        name: '（单位：分·秒）'
      },
      {
        grades: [5, 6],
        gender: 0,
        value: '50*8往返跑',
        key: 'girlEnduranceRun',
        name: '（单位：分·秒）'
      },
      {
        grades: [7, 8, 9, 10, 11, 12],
        gender: 1,
        value: '1000米跑',
        key: 'boyEnduranceRun',
        name: '（单位：分·秒）'
      },
      {
        grades: [7, 8, 9, 10, 11, 12],
        gender: 0,
        value: '800米跑',
        key: 'girlEnduranceRun',
        name: '（单位：分·秒）'
      },
    ],
    myTechnical: [],
    article: articles.bmi,
    standerd: [],
    sliTab: '', // 侧边栏切换
    unitKey: '',
    unitValue: '',
    grade: 7,
    gradeName:'',
    gender: 1
  },

  /**
   * 生命周期函数--监听页面加载
   * BMI，肺活量，50米，坐位体前屈，1分钟跳绳，立定跳远，一分钟仰卧起坐（引体向上），耐力跑（50*8，800米，1000米）
   */
  onLoad: function(options) {
    // 把学生信息的年级和性别传入
    var preArr = [];
    var name = '';
    this.data.technical.forEach(item => {
      if (item.grades.includes(this.data.grade) &&
        item.gender == this.data.gender) {
        preArr.push({
          value: item.value,
          key: item.key,
          name: item.name
        })
      }
    })
    var gender = this.data.gender == 1 ? '男生' : '女生';
    name = app.utils.formatGrade(this.data.grade) + gender + '标准';
    this.setData({
      myTechnical: preArr,
      sliTab: preArr[0].key,
      unitKey: name,
      unitValue: preArr[0].name,
      gradeName: app.utils.formatGrade(this.data.grade)
    })
    this.formatStandards(preArr[0].key);
    this.selectedArticle(preArr[0].key);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  // 滑动切换tab
  bindChange: function(e) {
    this.setData({
      curTab: e.detail.current
    })
  },
  // 点击tab切换
  swichNav: function(e) {
    if (this.data.curTab === e.target.dataset.current) {
      return false
    } else {
      this.setData({
        curTab: e.target.dataset.current
      })
    }
  },
  // 点击slider切换项目
  swichSlider: function(e) {
    if (this.data.sliTab === e.currentTarget.dataset.slider) {
      return false
    } else {
      // 根据学生性别和选择的项目筛选评分标准数据
      var slider = e.currentTarget.dataset.slider;
      this.formatStandards(slider);
      this.selectedArticle(slider)
      // 调整单位信息
      this.data.myTechnical.forEach(ele => {
        if (ele.key == slider) {
          this.setData({
            unitValue: ele.name
          })
        }
      })
      // var standTitle = 
      this.setData({
        sliTab: slider
      })
    }
  },
  // 筛选评分标准数据（表）
  formatStandards(slider) {
    var arrda = [];
    var num = 'g' + this.data.grade;

    stands[slider].forEach(ele => {
      arrda.push({
        level: ele.level,
        score: ele.score,
        benchmark: ele[num]
      })
    })

    this.setData({
      standerd: arrda
    })
  },
  // 筛选测试方法
  selectedArticle(sliTab) {
    // 绑定对应测试方法
    if (sliTab == 'boyEnduranceRun' || sliTab == 'girlEnduranceRun') {
      // 5,6年级对应50*8往返跑// 7-12对应1000/800米
      if (this.data.grade < 7) {
        this.setData({
          article: articles.run8_50
        })
      } else {
        this.setData({
          article: articles.run1000
        })
      }

    } else {
      var sliTabLower = sliTab.toLowerCase();
      for (var i in articles) {
        
        var iLower = i.toLowerCase()
        if (sliTabLower.indexOf(iLower) > -1) {
          console.log(i)
          return this.setData({
            article: articles[i]
          })
        }
      }
    }
  }

})